package co.fitstart.mobile.service.impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.demo2do.core.persistence.GenericDaoSupport;

import co.fitstart.entity.user.User;
import co.fitstart.entity.user.distributor.DistributionRecord;
import co.fitstart.entity.user.distributor.DistributionRecordType;
import co.fitstart.entity.user.distributor.Distributor;
import co.fitstart.entity.user.distributor.DistributorUserRelation;
import co.fitstart.mobile.service.DistributorService;

/**
 * 
 * @author mohanlan
 *
 */
@Service("distributorService")
public class DistributorServiceImpl implements DistributorService {
    
    @Autowired
    private GenericDaoSupport genericDaoSupport;
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.mobile.service.DistributorService#load(co.fitstart.entity.user.User)
     */
    public Distributor load(User user) {
        return genericDaoSupport.searchForObject("FROM Distributor WHERE user = :user AND disabled = false", "user", user, Distributor.class);
    }
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.mobile.service.DistributorService#saveQrcode(co.fitstart.entity.user.distributor.Distributor, java.lang.String)
     */
    public void saveQrcode(Distributor distributor, String qrcode) {
        genericDaoSupport.update(distributor.editQrcode(qrcode));
    }
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.mobile.service.DistributorService#createRelation(java.lang.Long, co.fitstart.entity.user.User)
     */
    public void createRelation(Long distributorId, User user) {
        
        Distributor distributor = genericDaoSupport.load(Distributor.class, distributorId);
        
        Map<String, Object> prameters = new HashMap<String, Object>();
        prameters.put("distributor", distributor);
        prameters.put("user", user);
        
        DistributorUserRelation relation = genericDaoSupport.searchForObject("FROM DistributorUserRelation WHERE distributor = :distributor AND user = :user AND consumed = false", prameters, DistributorUserRelation.class);
        
        if(relation == null) {
            relation = new DistributorUserRelation(distributor, user);
            genericDaoSupport.save(relation);
            
            genericDaoSupport.update(distributor.userFocus());
            
        }
    }
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.mobile.service.DistributorService#editAccount(co.fitstart.entity.user.distributor.Distributor, java.lang.String, java.lang.String, java.lang.String)
     */
    public void editAccount(Distributor distributor, String wechatId, String account, String realName) {
        genericDaoSupport.update(distributor.editAccount(wechatId, account, realName));
    }
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.mobile.service.DistributorService#withdraw(co.fitstart.entity.user.distributor.Distributor, java.math.BigDecimal)
     */
    public void withdraw(Distributor distributor, BigDecimal offer) {
        int offerFen = offer.multiply(new BigDecimal(100)).intValue();
        // save agent record
        DistributionRecord distributionRecord = new DistributionRecord(distributor, DistributionRecordType.WITHDRAWING, offerFen);
        genericDaoSupport.save(distributionRecord);
        
        // update agent withdrawl price
        genericDaoSupport.update(distributor.withdraw(offerFen));
    }
    
}
